/**	
 * タイトル：	Class操作类
 * 説明：    	
 *			
 * 会社名：	紹興日創軟件有限公司
 * 変更履歴：	2014/08/20 NIS 何
 *			新規作成
 *
 */
package com.nissoft.framework.util;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * @author NIS 何
 * @version 1.0
 */
public abstract class ClassUtil {

	public static final String CLASSPATH_URL_PREFIX = "classpath:";

	public static String getPath(String myPath) {
		return getPath(myPath, true);
	}

	/**
	 * 获取可能有classpath的真实路径
	 * 
	 * @param myPath
	 *            路径
	 * @param autoMkdirs
	 *            自动创建路径
	 */
	public static String getPath(String myPath, boolean autoMkdirs) {

		String retPath = myPath;
		if (myPath.startsWith(CLASSPATH_URL_PREFIX)) {
			try {
				String path = myPath.substring(CLASSPATH_URL_PREFIX.length());
				URL url = ClassUtil.getDefaultClassLoader().getResource(path);
				if (url == null) {
					String description = "class path resource [" + path + "]";
					LogUtil.warn(description
							+ " cannot be resolved to URL because it does not exist");
				} else {
					URI uri = url.toURI();
					File f = new File(uri);
					retPath = f.getAbsolutePath();
				}
			} catch (URISyntaxException e) {
			}
		}
		retPath = FileUtil.standardPath(retPath, true);
		if (autoMkdirs) {
			new File(retPath).mkdirs();
		}
		return retPath;
	}

	public static ClassLoader getDefaultClassLoader() {
		ClassLoader cl = null;
		try {
			cl = Thread.currentThread().getContextClassLoader();
		} catch (Throwable ex) {
		}
		if (cl == null) {
			cl = ClassUtil.class.getClassLoader();
		}
		return cl;
	}
}
